package com.gdc.microservicecloud.controller;

import com.gdc.microservicecloud.model.Dept;
import com.gdc.microservicecloud.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Description: 部门 Controller
 * @author: gdc
 * @date: 2021/1/17
 * @version 1.0
 */
@Slf4j
@RestController
public class DeptController {
  @Autowired
  private DeptService service;

  /**
   * 引入服务发现客户 Bean
   */
  @Autowired
  private DiscoveryClient discoveryClient;

  /**
   * 添加部门信息
   *
   * @param dept 部门信息
   * @return 结果
   */
  @PostMapping("/dept/add")
  public boolean add(@RequestBody Dept dept) {
    return service.add(dept);
  }

  /**
   * 通过ID 获取部门信息
   *
   * @param id ID
   * @return 部门信息
   */
  @GetMapping("/dept/get/{id}")
  public Dept get(@PathVariable("id") Long id) {
    return service.get(id);
  }

  /**
   * 获取部门信息列表
   *
   * @return 部门信息列表
   */
  @GetMapping("/dept/list")
  public List<Dept> list() {
    return service.list();
  }


  @GetMapping("/dept/discovery")
  public Object discovery(){
    List<String> services = discoveryClient.getServices();
    log.info("当前注册的服务包含：{}", services);

    discoveryClient.getInstances("microservicecloud-dept").forEach(instance ->
            log.info("实例信息，SERVICEID:{},HOST:{},PORT:{},URI:{},METADATA:{}",
              instance.getServiceId(),
              instance.getHost(),
              instance.getPort(),
              instance.getUri(),
              instance.getMetadata()));
    return discoveryClient;
  }

}